<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品类型" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择商品类型" clearable size="small">
          <el-option
            v-for="product in productType"
            :key="product.value"
            :label="product.label"
            :value="product.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="面额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:product:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:product:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:product:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:product:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="productType" :formatter="productTypeFormat"/>
      <el-table-column label="面额" align="center" prop="money"/>
      <el-table-column :label="queryParams.productType==1?'成本价(移动)':'成本价'" align="center" prop="mobileMoneyCost"/>
      <el-table-column :label="queryParams.productType==1?'售价(移动)':'售价'" align="center" prop="mobileMoneyPrice"/>
      <!--            <el-table-column label="商品ID(移动)" align="center" prop="mobileFuluProductId" v-if="queryParams.productType==1"/>-->
      <el-table-column label="成本价联通)" align="center" prop="unicomMoneyCost" v-if="queryParams.productType==1"/>
      <el-table-column label="售价(联通)" align="center" prop="unicomMoneyPrice" v-if="queryParams.productType==1"/>
      <!--            <el-table-column label="商品ID(联通)" align="center" prop="unicomFuluProductId" v-if="queryParams.productType==1"/>-->
      <el-table-column label="成本价电信)" align="center" prop="telecomMoneyCost" v-if="queryParams.productType==1"/>
      <el-table-column label="售价(电信)" align="center" prop="telecomMoneyPrice" v-if="queryParams.productType==1"/>
      <!--            <el-table-column label="商品ID(电信)" align="center" prop="telecomFuluProductId" v-if="queryParams.productType==1"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:product:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:product:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改话费商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品类型" prop="productType">
              <el-select v-model="form.productType" placeholder="请选择商品类型" clearable size="small">
                <el-option
                  v-for="product in productType"
                  :key="product.value"
                  :label="product.label"
                  :value="product.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="面额" prop="money">
              <el-input-number v-model="form.money" :min="10" :max="100000" :step="10" :step-strictly="true"
                               placeholder="请输入面额"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider v-if="form.productType==1||form.productType==2"><i class="el-icon-mobile-phone"></i>移动</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="成本价" prop="mobileMoneyCost">
              <el-input v-model="form.mobileMoneyCost" placeholder="请输入成本价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="售价" prop="mobileMoneyPrice">
              <el-input v-model="form.mobileMoneyPrice" placeholder="请输入售价"/>
            </el-form-item>
          </el-col>
          <!--                    <el-col :span="8" v-if="form.productType==1||form.productType==2">-->
          <!--                        <el-form-item label="商品编号" prop="mobileFuluProductId">-->
          <!--                            <el-input v-model="form.mobileFuluProductId" placeholder="请输入福禄平台商品编号"/>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品说明" prop="mobileFuluProductName">
              <el-input v-model="form.mobileFuluProductName" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider v-if="form.productType==1||form.productType==2"><i class="el-icon-mobile-phone"></i>联通</el-divider>
        <el-row v-if="form.productType==1||form.productType==2">
          <el-col :span="8">
            <el-form-item label="成本价" prop="unicomMoneyCost">
              <el-input v-model="form.unicomMoneyCost" placeholder="请输入成本价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="售价" prop="unicomMoneyPrice">
              <el-input v-model="form.unicomMoneyPrice" placeholder="请输入售价"/>
            </el-form-item>
          </el-col>
          <!--                    <el-col :span="8">-->
          <!--                        <el-form-item label="商品编号" prop="unicomFuluProductId">-->
          <!--                            <el-input v-model="form.unicomFuluProductId" placeholder="请输入福禄平台商品编号"/>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
        </el-row>
        <el-row v-if="form.productType==1||form.productType==2">
          <el-col :span="24">
            <el-form-item label="商品说明" prop="unicomFuluProductName">
              <el-input v-model="form.unicomFuluProductName" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider v-if="form.productType==1||form.productType==2"><i class="el-icon-mobile-phone"></i>电信</el-divider>
        <el-row v-if="form.productType==1||form.productType==2">
          <el-col :span="8">
            <el-form-item label="成本价" prop="telecomMoneyCost">
              <el-input v-model="form.telecomMoneyCost" placeholder="请输入成本价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="售价" prop="telecomMoneyPrice">
              <el-input v-model="form.telecomMoneyPrice" placeholder="请输入售价"/>
            </el-form-item>
          </el-col>
          <!--                    <el-col :span="8">-->
          <!--                        <el-form-item label="商品编号" prop="telecomFuluProductId">-->
          <!--                            <el-input v-model="form.telecomFuluProductId" placeholder="请输入福禄平台商品编号"/>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
        </el-row>
        <el-row v-if="form.productType==1||form.productType==2">
          <el-col :span="24">
            <el-form-item label="商品说明" prop="telecomFuluProductName">
              <el-input v-model="form.telecomFuluProductName" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from '@/api/app/product'

  export default {
    name: 'Product',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        productType: [
          { value: 1, label: '慢充话费' },
          { value: 2, label: '快充话费' },
          { value: 4, label: '电费' },
          { value: 5, label: '水费' },
          { value: 6, label: '燃气费' },
          { value: 7, label: '油卡' },
          { value: 10, label: '活动商品' }
        ],
        // 话费商品表格数据
        productList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productType: null,
          productTypes: '11,12,13,17,18,16',
          money: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          money: [
            { required: true, message: '面额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 商品类型翻译
      productTypeFormat(row, column) {
        let productType = this.productType.find(item => item.value == row.productType)
        if (productType) {
          return productType.label
        }
        return ''
      },
      /** 查询话费商品列表 */
      getList() {
        this.loading = true
        listProduct(this.queryParams).then(response => {
          this.productList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          billId: null,
          money: null,
          mobileMoneyCost: null,
          mobileMoneyPrice: null,
          mobileFuluProductId: null,
          mobileFuluProductName: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          unicomMoneyCost: null,
          unicomMoneyPrice: null,
          unicomFuluProductId: null,
          unicomFuluProductName: null,
          telecomMoneyCost: null,
          telecomMoneyPrice: null,
          telecomFuluProductId: null,
          telecomFuluProductName: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.billId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加商品'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const billId = row.billId || this.ids
        getProduct(billId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改商品'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.billId != null) {
              updateProduct(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addProduct(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const billIds = row.billId || this.ids
        this.$confirm('是否确认删除商品编号为"' + billIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delProduct(billIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有商品数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportProduct(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
